/**
 * 
 */
package com.zhaokuo.controller;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.zhaokuo.config.FileUpload;
import com.zhaokuo.model.User;
import com.zhaokuo.service.UserService;
import com.zhaokuo.util.ConvertUtil;
import com.zhaokuo.util.FileUtil;
import com.zhaokuo.util.WebUtils;

/**
 * @author zk 2012-3-16
 *
 */
@Controller
public class HelloWordController {
	
	@Autowired
	private UserService userService;
	@Autowired
	private FileUpload fileUpload;
	@RequestMapping("/hello")
	 public String  helloWorld(ModelMap model) {
	  String message = "Hello World, Spring 3.0!";
	  System.out.println(message);
	  	long id =1;
	  	User user =userService.select(id);
	  	List<User> ulist= userService.selectAll();
	  	System.out.println(user+"\t"+ulist.size());
	  return "log";
	}
	@RequestMapping("/thymealef")
	public String thymealeafTest(ModelMap model){
		System.out.println(123);
		return "index";
		
	}
	
	@RequestMapping("/velocity")
	public String velocity(ModelMap model){
		System.out.println(123);
		model.put("name", "zhaokuo");
		return "index";
		
	}
	@RequestMapping(value="/fileUp/list",method=RequestMethod.POST)
	public void fileUp(HttpServletRequest request,HttpServletResponse response,@RequestParam(required=false) MultipartFile file){
		int result=0;

		String date = ConvertUtil.convertDateToStr(new Date(), "yyyyMMdd");
		long time = System.currentTimeMillis();
		System.out.println(FileUtil.getFileSeparator());
		String imageName = date + time;
		String dir="C:/temp";
		String filename=imageName+".jpg";
		if(!FileUtil.validImgSuffix(file)){
			result=2;
		}else {
			try {
				fileUpload.upload(file, dir, filename);
				result=1;
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
		String script=WebUtils.getJavaScript("upreslut("+result+")");
		WebUtils.response(response, script);
	}
}
